﻿using System.Collections.Generic;
using System;

namespace Efficient.Domain
{
    [Serializable]
    public abstract class RawTreeNodeEntity<T>
    {
        protected ICollection<T> childs = new List<T>();
        protected T parent;

        public virtual ICollection<T> Childs
        {
            get { return childs; }
            set { childs = value; }
        }

        public virtual T Parent
        {
            get
            {
                if (AssertParentNeedCreate())
                {
                    parent = CreateParentProxy();
                }
                return parent;
            }
            set { parent = value; }
        }

        protected virtual bool AssertParentNeedCreate()
        {
            return AssertParentIdIsNotEmpty() && parent == null;
        }

        protected abstract T CreateParentProxy();

        protected abstract bool AssertParentIdIsNotEmpty();
    }
}